package com.glink.manage.common;

import com.glink.common.util.BaseCommonUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2025/3/11 11:16
 */
@Slf4j
public class CardUtil {


    /**
     * 提取生日
     * @param card 身份证号
     * @return 生日
     */
    public static Date extractBirthDate(String card){
        if (card == null || card.length() != 18) {
            return null;
        }
        // 提取出生日期
        String birthDateStr = card.substring(6, 14);
        Date parse = null;
        try {
            parse = BaseCommonUtils.DATE_FORMAT_DAY_2.parse(birthDateStr);
        } catch (ParseException e) {
            log.info("身份证号格式转换失败", e);
            birthDateStr = birthDateStr.substring(0, 4) + "-" + birthDateStr.substring(4, 6) + "-" + birthDateStr.substring(6);
            try {
                parse = BaseCommonUtils.DATE_FORMAT_DAY_3.parse(birthDateStr);
            } catch (ParseException ex) {
                log.info("身份证号格式转换失败", ex);
            }
        }
        return parse;
    }


    /**
     *  提取性别
     * @param card 身份证号
     * @return 性别
     */
    public static String extractSex(String card){

        if (card == null || card.length() != 18) {
            return "";
        }
        // 提取性别
        char genderChar = card.charAt(16);
        return  (genderChar % 2 == 0) ? "2" : "1";
    }
}
